<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//error_reporting(0);
header('Content-Type: text/html; charset=utf-8');

class Admin_promotion extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('session');
        $this->load->model('users_model');
        $this->load->model('admin_category_model');
        $this->load->model('admin_product_model');
        $this->load->model('admin_promotion_model');
        $this->load->model('crud_model');
        $this->load->library('../controllers/crud');
        $this->load->library("pagination");
    }

    public function getTablePromotionManager($start_from = 0)
    {
        $per_page = 4;
        $config = array();
        $config["base_url"] = base_url() . "/admin_promotion/getTablePromotionManager/";
        $config["total_rows"] = $this->crud_model->selectNumOfPagination("*", "promotion AS pro", "pro.flag = 0 ORDER BY pro.create_datetime DESC");
        $config["per_page"] = $per_page;
        $config["uri_segment"] = 3;
        $config['full_tag_open'] = '<ul>';
        $config['full_tag_close'] = '</ul>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active">';
        $config['cur_tag_close'] = '</li> ';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li> ';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li> ';
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = round($choice);
        $config['use_page_numbers'] = TRUE;
        $start = $per_page * ($start_from-1);
        if($start < 0){
            $start = 0;
        }
        $data['promotionList'] = $this->crud_model->selectPagination($per_page, $start, "*", "promotion AS pro", "pro.flag = 0 ORDER BY pro.create_datetime DESC");
        $this->load->view('admin/promotion/promotion_list', $data);
    }

    public function getPage()
    {
        $result = $this->crud_model->selectNumOfPagination("*", "promotion AS pro", "pro.flag = 0 ORDER BY pro.create_datetime DESC");
        $data['num_links'] = $result/4;
        $this->load->view('admin/promotion/page', $data);
    }

    public function createNewPromotion(){
        $dt = date('Y-m-d H:i:s');
        $date = date('Y-m-d');

        $promotionName = $this->input->post('promotionName', true);
        $promotionStatus = $this->input->post('promotionStatus', true);
        $promotionFrom = $this->input->post('promotionFrom', true);
        $promotionTo = $this->input->post('promotionTo', true);
        $buying_bouts = $this->input->post('buying_bouts', true);
        $other_programs = $this->input->post('other_programs', true);
        $promotionInform = $this->input->post('promotionInform', true);
        $buying_value = $this->input->post('buying_value', true);
        $getting_value = $this->input->post('getting_value', true);
        $detail_style = $this->input->post('detail_style', true);
        $promotion_style = $this->input->post('promotion_style', true);
        $value = $this->input->post('value', true);
        $product_apply = null;

        if(in_array($promotion_style, array('5','6','7'), true)){ //kiem tra ton tai trong 1 mang dung php
            $myArr = explode(",", $value);
            foreach($myArr as $objMyArr){
                $id = $this->getIDProductByName($objMyArr);
                $product_apply .= $id.",";
            }
            $product_apply = substr($product_apply, 0, strlen($product_apply)-1);
        }

        if($promotion_style == 8){
            $product_apply = $value;
        }

        $idUser = $this->crud->getIDUser();

        if($promotionFrom == 'null'){
            $promotionFrom = $date;
            $promotionTo = $date;
        }

        if($buying_bouts == 'null'){
            $buying_bouts = 0;
        }

        if($buying_value == 'null'){
            $buying_value = 0;
        }

        if($getting_value == 'null'){
            $getting_value = 0;
        }

        if($promotion_style == 4){
            $getting_value = $this->getIDProductByName($getting_value);
        }

        //khong duoc dung id lam ten cot => loi unknown column
        $data = array(
            "promotion_name" => $promotionName,
            "promotion_status" => $promotionStatus,
            "promotion_from" => $promotionFrom,
            "promotion_to" => $promotionTo,
            "buying_bouts" => $buying_bouts,
            "other_programs" => $other_programs,
            "buying_value" => $buying_value,
            "getting_value" => $getting_value,
            "detail_style" => $detail_style,
            "product_apply" => $product_apply,
            "promotion_style" => $promotion_style,
            "promotion_inform" => $promotionInform,
            "create_datetime" => $dt,
            "flag" => 0,
            "id_user" => $idUser
        );

        echo $this->crud_model->insert("promotion", $data);
    }

    public function deletePromotion(){
        $id = $this->input->post('id');
        $query = $this->crud_model->deleteID('promotion_id', $id, "promotion");
        echo $query;
    }

    public function updatePromotion(){
        $dt = date('Y-m-d H:i:s');
        $date = date('Y-m-d');

        $id = $this->input->post('id', true);
        $promotionName = $this->input->post('promotionName', true);
        $promotionStatus = $this->input->post('promotionStatus', true);
        $promotionFrom = $this->input->post('promotionFrom', true);
        $promotionTo = $this->input->post('promotionTo', true);
        $buying_bouts = $this->input->post('buying_bouts', true);
        $other_programs = $this->input->post('other_programs', true);
        $promotionInform = $this->input->post('promotionInform', true);
        $buying_value = $this->input->post('buying_value', true);
        $getting_value = $this->input->post('getting_value', true);
        $detail_style = $this->input->post('detail_style', true);
        $promotion_style = $this->input->post('promotion_style', true);
        $value = $this->input->post('value', true);
        $product_apply = null;

        if(in_array($promotion_style, array('5','6','7'), true)){ //kiem tra ton tai trong 1 mang dung php
            $myArr = explode(",", $value);
            foreach($myArr as $objMyArr){
                $idValue = $this->getIDProductByName($objMyArr);
                $product_apply .= $idValue.",";
            }
            $product_apply = substr($product_apply, 0, strlen($product_apply)-1);
        }

        if($promotion_style == 8){
            $product_apply = $value;
        }

        if($promotionFrom == 'null'){
            $promotionFrom = $date;
            $promotionTo = $date;
        }

        if($buying_bouts == 'null'){
            $buying_bouts = 0;
        }

        if($buying_value == 'null'){
            $buying_value = 0;
        }

        if($getting_value == 'null'){
            $getting_value = 0;
        }

        if($promotion_style == 4){
            $getting_value = $this->getIDProductByName($getting_value);
        }

        $data = array(
            "promotion_name" => $promotionName,
            "promotion_status" => $promotionStatus,
            "promotion_from" => $promotionFrom,
            "promotion_to" => $promotionTo,
            "buying_bouts" => $buying_bouts,
            "other_programs" => $other_programs,
            "buying_value" => $buying_value,
            "getting_value" => $getting_value,
            "detail_style" => $detail_style,
            "product_apply" => $product_apply,
            "promotion_style" => $promotion_style,
            "promotion_inform" => $promotionInform,
            "update_datetime" => $dt
        );

        echo $this->crud_model->update("promotion_id", $id, "promotion", $data);
    }

    public function getAddParentCategory($i, $id)
    {
//        echo $id;
        $data['menu'] = $i;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/promotion/add_category', $data);
    }

    public function getAddCategoryProduct()
    {
        $id_category = $this->input->post('idCate', true);
        if($id_category != 0){
            $result = $this->admin_product_model->getAddCategoryProduct($id_category);
        }
        else{
            $result = $this->admin_product_model->getAllCategoryProduct();
        }
        $string = "";
        foreach($result as $objResult){
            $string .= $objResult->product_name.", ";
        }
        echo $string;
    }

    public function getEditParentCategory($i, $id)
    {
//        echo $id;
        $data['menu'] = $i;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/promotion/edit_category', $data);
    }

    public function getIDProductByName($productName){
        $field = "id";
        $id = $this->admin_product_model->getIDProductByName($field, $productName);
        return $id;
    }

    public function getNameProductByID($isCate){
        $productID = $this->input->post('productID', true);
        $field = "product_name";
        if($isCate != 1){
            $name = $this->admin_product_model->getNameProductByID($field, $productID);
        }
        else{
            $field1 = "name";
            $name = $this->admin_category_model->getNameCategoryByID($field1, $productID);
        }
        echo $name;
    }
}